<?
class JCAT_ItemIterator
{
	function JCAT_ItemIterator($aModel,$aItem=null)
	{
		ASSERT( $this->aModel = $aModel ) ;
		$this->aThisItem = $aItem ;
		
		// $this->First() ;
	}
	
	protected $aModel ;
	protected $aThisItem ;
	protected $aCurrentChildItem ;
	protected $aCurrentChildIterator ;
	
	public function First()
	{
		$this->aCurrentChildItem = $this->aModel->iter_children( $this->aThisItem ) ;
		$this->CreateChildIterator() ;

		$this->aCurrentChildItem ;
		
		return $this->Current() ;
	}
	
	public function Next()
	{
		if($this->IsDone())
			return null ;

		if( $this->aCurrentChildIterator and !$this->aCurrentChildIterator->IsDone() )			// 优先 迭代子节点
		{
			return $this->aCurrentChildIterator->Next() ;
		}
		else
		{
			$aRet = $this->aCurrentChildItem ;
			$this->aCurrentChildItem = $this->aModel->iter_next($this->aCurrentChildItem) ;
			$this->CreateChildIterator() ;
			return $aRet ;
		}
	}
	
	public function Current()
	{
		if( $this->aCurrentChildIterator and !$this->aCurrentChildIterator->IsDone() )
			return $this->aCurrentChildIterator->Current() ;
		else
			return $this->aCurrentChildItem ;
	}
	
	public function IsDone()
	{
		return ($this->aCurrentChildItem==null) ;
	}
	
	private function CreateChildIterator()
	{
		if( $this->aCurrentChildItem )
		{
			$this->aCurrentChildIterator = new JCAT_ItemIterator($this->aModel,$this->aCurrentChildItem) ;
			$this->aCurrentChildIterator->First() ;
		}
		else
			$this->aCurrentChildIterator = null ;
	}
	
	//static function Create($aModel)
	//{ return new JCAT_ItemIterator($aModel, $aModel->get_iter_root()) ; }
}
?>